package org.zlpt.contract.dao;

import com.owlike.genson.Genson;
import org.hyperledger.fabric.contract.Context;
import org.hyperledger.fabric.contract.ContractInterface;
import org.hyperledger.fabric.contract.annotation.Contract;
import org.hyperledger.fabric.contract.annotation.Transaction;
import org.hyperledger.fabric.shim.ChaincodeException;
import org.hyperledger.fabric.shim.ChaincodeStub;
import org.zlpt.entity.co.SupervisionDepartmentCO;


@Contract(
        name = "supervisionDepartment"
)
public final class SupervisionDepartmentContract implements ContractInterface {
    private final Genson genson = new Genson();

    @Transaction(intent = Transaction.TYPE.EVALUATE)
    public boolean SupervisionUserExists(final Context ctx, String supervisionUserId){
        ChaincodeStub stub = ctx.getStub();
        SupervisionDepartmentCO supervisionUser = genson.deserialize(supervisionUserId, SupervisionDepartmentCO.class);
        String supervisionUserJSON = stub.getStringState(supervisionUser.formKey());

        return (supervisionUserJSON != null && !supervisionUserJSON.isEmpty());
    }

    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public SupervisionDepartmentCO CreateSupervisionUser(final Context ctx, String supervisionUserJson) {
        ChaincodeStub stub = ctx.getStub();
        if (SupervisionUserExists(ctx, supervisionUserJson)) {
            String errorMessage = String.format("SupervisionUser already exists");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }
        SupervisionDepartmentCO supervisionUser = genson.deserialize(supervisionUserJson, SupervisionDepartmentCO.class);
        stub.putStringState(supervisionUser.formKey(), supervisionUserJson);
        return supervisionUser;
    }
    @Transaction(intent = Transaction.TYPE.EVALUATE)
    public SupervisionDepartmentCO ReadSupervisionUser(final Context ctx, String supervisionUserId) {
        ChaincodeStub stub = ctx.getStub();
        String keyPrefiexSupervisionUserId = SupervisionDepartmentCO.formKey(Long.valueOf(supervisionUserId));
        String supervisionUserJSON = stub.getStringState(keyPrefiexSupervisionUserId);
        if (supervisionUserJSON == null || supervisionUserJSON.isEmpty()) {
            String errorMessage = String.format("SupervisionUser does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }

        SupervisionDepartmentCO supervisionUser = genson.deserialize(supervisionUserJSON, SupervisionDepartmentCO.class);
        return supervisionUser;
    }
    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public SupervisionDepartmentCO UpdateSupervisionUser(final Context ctx, String supervisionUserJson) {
        ChaincodeStub stub = ctx.getStub();
        SupervisionDepartmentCO supervisionUser = genson.deserialize(supervisionUserJson, SupervisionDepartmentCO.class);

        if (!SupervisionUserExists(ctx, supervisionUserJson)) {
            String errorMessage = String.format("SupervisionUser does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }
        String oldSupervisionUserJson = stub.getStringState(supervisionUser.formKey());
        SupervisionDepartmentCO oldSupervisionUser = genson.deserialize(oldSupervisionUserJson, SupervisionDepartmentCO.class);
        String sortedJson = genson.serialize(supervisionUser);
        stub.putStringState(supervisionUser.formKey(), sortedJson);
        return oldSupervisionUser;
    }
    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public void DeleteSupervisionUser(final Context ctx, String supervisionUserId) {
        ChaincodeStub stub = ctx.getStub();

        if (!SupervisionUserExists(ctx, supervisionUserId)) {
            String errorMessage = String.format("SupervisionUser does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }

        stub.delState(supervisionUserId);
    }
}






